#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

bool compare(const pair<string, int>& a, const pair<string, int>& b)
{
	if (a.second == b.second)
	{
		return a.first < b.first;
	}
	return a.second > b.second;
}

int main()
{
	int n;
	cin >> n;

	map<string, int> students;
	for (int i = 0;i < n;i++)
	{
		string name;
		int score;
		cin >> name >> score;
		students[name] = score;
	}

	vector<pair<string, int>> studentVec(students.begin(), students.end());

	sort(studentVec.begin(), studentVec.end(), compare);

	for (const auto& student : studentVec)
	{
		cout << student.first << " " << student.second << endl;
	}

	return 0;
}